package com.xq.tmall.service;


import com.xq.tmall.agu.job.ITimerJob;
import com.xq.tmall.dao.StockSectorHistoryDao;
import com.xq.tmall.entity.Stock;
import com.xq.tmall.entity.StockSector;
import com.xq.tmall.entity.StockSectorHistory;
import com.xq.tmall.util.BeanUtil;
import com.xq.tmall.util.ITimeJobFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class StockSectorHistoryService {

    @Resource
    private StockSectorHistoryDao sectorHistoryDao;

    public void batchInsert(List<StockSector> sectorList) throws Exception {
        List<StockSectorHistory> stockSectorHistoryList = new ArrayList<>();
        sectorList.stream().forEach(stockSector -> {
            StockSectorHistory stockSectorHistory = new StockSectorHistory();
            try {
                BeanUtils.copyProperties(stockSector,stockSectorHistory);
            }catch (Exception e){
                log.info("转换失败",e );
            }
            stockSectorHistory.setPriceDate(DateUtils.formatDate(new Date(), "yyyy-MM-dd"));
            stockSectorHistoryList.add(stockSectorHistory);
        });
        List<List<StockSectorHistory>> listList = BeanUtil.splitsList(stockSectorHistoryList, 500);
        for (List<StockSectorHistory> list : listList) {
            sectorHistoryDao.batchInsert(list);
        }
    }

    public void historyDelete(String historyDate) {
        sectorHistoryDao.historyDelete(historyDate);
    }

    public List<StockSectorHistory> findStockSectorHistoryList(Map<String,Object> params) {
        List<StockSectorHistory> stockHistoryList = sectorHistoryDao.findStockSectorHistoryList(params);
        return stockHistoryList;
    }
}
